package com.huirui.server.controller.mini.program;

import com.huirui.server.context.UserContext;
import com.huirui.server.pojo.dto.CollectionDto;
import com.huirui.server.pojo.entity.ProductFavorite;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.ProductBriefInfoVo;
import com.huirui.server.service.ProductFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/favorite")
@Slf4j
@Validated
@Tag(name = "商品收藏管理", description = "微信小程序商品收藏相关")
public class ProductFavoriteController {
    @Resource
    private ProductFavoriteService productFavoriteService;

    @GetMapping
    @Operation(summary = "获取用户的收藏商品")
    public Result<List<ProductBriefInfoVo>> listUserFavoriteProduct() {
        List<ProductBriefInfoVo> list = productFavoriteService.listUserFavoriteProduct();
        return Result.success(list);
    }


    @GetMapping("/list")
    @Operation(summary = "获取用户收藏列表")
    public Result<PageResult<ProductBriefInfoVo>> listProductFavoriteProduct(Long page, Long pageSize) {
        PageResult<ProductBriefInfoVo> res = productFavoriteService.listProductFavoriteProduct(page, pageSize);
        return Result.success(res);
    }


    @PostMapping
    @Operation(summary = "添加商品到收藏")
    public Result<Void> addProductToCollection(@RequestBody CollectionDto collectionDto) {
        ProductFavorite productFavorite = new ProductFavorite();
        productFavorite.setProductId(collectionDto.getProductId());
        productFavorite.setFavoriteTime(LocalDateTime.now());
        productFavorite.setUserId(UserContext.getId());
        productFavoriteService.save(productFavorite);
        return Result.success();
    }

    @DeleteMapping("/{productId}")
    @Operation(summary = "取消商品收藏")
    public Result<Void> removeProductFavorite(@PathVariable Long productId) {
        productFavoriteService
                .lambdaUpdate()
                .eq(ProductFavorite::getProductId, productId)
                .eq(ProductFavorite::getUserId, UserContext.getId())
                .remove();
        return Result.success();
    }

    @GetMapping("/{productId}")
    @Operation(summary = "获取某商品是否为收藏商品")
    public Result<Boolean> queryIsFavoriteProduct(@PathVariable Long productId) {
        boolean exists = productFavoriteService.lambdaQuery()
                .eq(ProductFavorite::getProductId, productId)
                .eq(ProductFavorite::getUserId, UserContext.getId())
                .exists();
        return Result.success(exists);
    }

    @DeleteMapping
    @Operation(summary = "批量删除商品收藏")
    public Result<Void> removeProductFavoriteByProductIds(@RequestParam("ids") List<Long> ids) {
        productFavoriteService
                .lambdaUpdate()
                .eq(ProductFavorite::getUserId, UserContext.getId())
                .in(ProductFavorite::getProductId, ids)
                .remove();
        return Result.success();
    }
}
